package com.example.milktea.dao.Impl;

import com.example.milktea.dao.StoreDao;
import com.example.milktea.entity.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;

@Repository
public class StoreDaoImpl implements StoreDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

   /*
    @Override
    public int insert(Store store) {
        //新增的方法
        String sql = "insert into store (material_id,store_number,lack_flag) values (?,?,?)";
        return this.jdbcTemplate.update(
                sql,
                store.getMaterial_id(),
                store.getStoreNumber(),
                store.isLack_flag()
        );
    }
    */
    public int update(Store store){
        //添加对缺货标记lack_flag字段的更新
        String sql = "update store set store_number = ? where store_id = ?";
        int returnValue=  this.jdbcTemplate.update(
                sql,
                store.getStoreNumber(),
                store.getStoreId()
        );
        String sql1 = "update store set lack_flag=1 where store_number < 10";
        System.out.println("1:"+this.jdbcTemplate.update(sql1));
        String sql2 = "update store set lack_flag=0 where store_number >= 10";
        System.out.println("2:"+this.jdbcTemplate.update(sql2));
        return returnValue;
    }

    public List<Map<String,Object>> getAll(){
        //更改了获取的数据内容
        String sql = "select store_id,material_name,store_number,supplement_price,supplement_phone,lack_flag from material natural join store";
        //String sql = "select store_id,material_id,store_number,lack_flag from store";
        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    @Override
    public List<Map<String, Object>> getAllLack() {
        //新增
        String sql = "select store_id,material_name,store_number,supplement_price,supplement_phone,lack_flag from material natural join store where lack_flag=1";
        List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    public int query(String store_id) {
        String sql = "select count(store_number) from store where store_id = ? ";
        return this.jdbcTemplate.queryForObject(sql,new Object[]{store_id},Integer.class);
    }

    public void addMaterial(int store_id) {
        String sql = "update store set store_number = store_number+1 where store_id = ?";
        this.jdbcTemplate.update(sql,store_id);
    }

    public void deleteMaterial(int store_id) {
        String sql = "update store set store_number = store_number-1 where store_id = ?";
        this.jdbcTemplate.update(sql,store_id);
    }
}
